package com.example.droideep.fragment.observable;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;

import com.example.droideep.R;
import com.example.droideep.common.dummydata.Cheeses;
import com.github.ksoichiro.android.observablescrollview.ObservableListView;
import com.github.ksoichiro.android.observablescrollview.ObservableScrollViewCallbacks;
import com.github.ksoichiro.android.observablescrollview.ScrollState;
import com.github.ksoichiro.android.observablescrollview.ScrollUtils;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;

/**
 * @author bri
 * @date 15-5-11.
 */
public class ObservableVu implements IVu, ObservableScrollViewCallbacks {

    private View rootView;

    private ObservableListView mListView;

    private View mHeaderView;
    private int mBaseTranslationY;

    @Override
    public void init(LayoutInflater inflater, ViewGroup container) {
        rootView = inflater.inflate(R.layout.fragment_observable_list_view, container, false);
        mListView = (ObservableListView) rootView.findViewById(android.R.id.list);

        mHeaderView = rootView.findViewById(R.id.header_view);

        mListView.addHeaderView(inflater.inflate(R.layout.header_view, mListView, false));
        mListView.setScrollViewCallbacks(this);
        mListView.setAdapter(new ArrayAdapter<String>(
                inflater.getContext(),
                android.R.layout.simple_list_item_1,
                android.R.id.text1,
                Cheeses.CHEESES));
    }

    @Override
    public View getView() {
        return rootView;
    }

    @Override
    public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging) {
        if (dragging) {
            int headerViewHeight = mHeaderView.getHeight();
            if (firstScroll) {
                float currentHeaderTranslationY = mHeaderView.getTranslationY();
                if (-headerViewHeight < currentHeaderTranslationY) {
                    mBaseTranslationY = scrollY;
                }
            }
            float headerTranslationY = ScrollUtils.getFloat(-(scrollY - mBaseTranslationY), -headerViewHeight, 0);
            ViewPropertyAnimator.animate(mHeaderView).cancel();
            ViewHelper.setTranslationY(mHeaderView, headerTranslationY);
        }
    }

    @Override
    public void onDownMotionEvent() {

    }

    @Override
    public void onUpOrCancelMotionEvent(ScrollState scrollState) {
        mBaseTranslationY = 0;

        if (scrollState == ScrollState.DOWN) {
            showHeaderView();
        } else if (scrollState == ScrollState.UP) {
            int toolbarHeight = mHeaderView.getHeight();
            int scrollY = mListView.getCurrentScrollY();
            if (toolbarHeight <= scrollY) {
                hideHeaderView();
            } else {
                showHeaderView();
            }
        } else {
            // Even if onScrollChanged occurs without scrollY changing, toolbar should be adjusted
            if (!toolbarIsShown() && !toolbarIsHidden()) {
                // Toolbar is moving but doesn't know which to move:
                // you can change this to hideHeaderView()
                showHeaderView();
            }
        }
    }

    private boolean toolbarIsShown() {
        return ViewHelper.getTranslationY(mHeaderView) == 0;
    }

    private boolean toolbarIsHidden() {
        return ViewHelper.getTranslationY(mHeaderView) == -mHeaderView.getHeight();
    }

    private void showHeaderView() {
        float headerTranslationY = ViewHelper.getTranslationY(mHeaderView);
        if (headerTranslationY != 0) {
            ViewPropertyAnimator.animate(mHeaderView).cancel();
            ViewPropertyAnimator.animate(mHeaderView).translationY(0).setDuration(200).start();
        }
    }

    private void hideHeaderView() {
        float headerTranslationY = ViewHelper.getTranslationY(mHeaderView);
        int toolbarHeight = mHeaderView.getHeight();
        if (headerTranslationY != -toolbarHeight) {
            ViewPropertyAnimator.animate(mHeaderView).cancel();
            ViewPropertyAnimator.animate(mHeaderView).translationY(-toolbarHeight).setDuration(200).start();
        }
    }
}
